<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2017/4/28
 * Time: 10:53
 */

namespace app\models;

use app\librarys\DbCacheUtility;


/**
 * 权限
 * Class Permission
 * @package app\models
 */
class Permission
{
    public static function hasPermission($controllerId, $actionId)
    {
        $currentManager = Manager::getCurrentManager();
        $hasPermission = false;
        if ($currentManager != null) {
            if ($currentManager['id'] == '0') {  // 系统超级管理员
                $hasPermission = true;
            } else {
                if (isset($controllerId) && isset($actionId)) {

                    $noAuthActions = self::_noAuthActions();
                    foreach ($noAuthActions as $noAuthAction) {
                        if ($noAuthAction['controller_id'] == $controllerId && $noAuthAction['action_id'] == $actionId) {
                            $hasPermission = true;
                            break;
                        }
                    }

                    if ($hasPermission == false) {
                        foreach ($currentManager['permission'] as $item) {
                            if ($item['controller_id'] == $controllerId && $item['action_id'] == $actionId) {
                                $hasPermission = true;
                                break;
                            }
                        }
                    }

                }
            }
        }
        return $hasPermission;
//        return true;
    }

    /**
     * 无需鉴权即可访问的action
     * @return array
     */
    private static function _noAuthActions()
    {
        return [
            ['controller_id' => 'system', 'action_id' => 'person-info-partview'],  // 管理员个人信息编辑修改
            ['controller_id' => 'system', 'action_id' => 'modify-password-handler'],  // 管理员对各人的密码进行修改handler
            ['controller_id' => 'system', 'action_id' => 'modify-manager-info-handler'],  // 管理员个人信息编辑修改handler
        ];
    }
}